"""Function for generate file with clang options"""
import argparse
import os
from jinja2 import Environment, FileSystemLoader

target_list = (
    'aarch64-linux-hos',
    'arm-linux-ohos',
    'aarch64-linux-ohos',
    'arm-liteos',
    'arm-linux-hos',
    'riscv64-linux-ohos',
    'x86_64-linux-hos',
    'x86_64-linux-gnu',
    'x86_64-w64-mingw32',
    'aarch64-linux-android29',
    'aarch64-linux-gnu',
)

def init_parser():
    """init parser with arguments and returns it"""
    _parser = argparse.ArgumentParser()
    _parser.add_argument('--target', nargs='?',
                         choices=target_list,
                         required=True, help='Target')
    _parser.add_argument('--default', required=True, help='Path to folder with default config')
    _parser.add_argument('--clang', required=False, help='Path to toolchain')
    _parser.add_argument('--sysroot', required=False, help='Path to sysroot')
    _parser.add_argument('--out', required=False, default=os.getcwd(),
                         help='Path where generated config will be placed (default: workdir')
    return _parser

def main():
    """Main script entrypoint
    :return:
    """
    parser = init_parser()
    args = parser.parse_args()

    config_filename = '{}.cfg'.format(args.target)
    config_file = os.path.join(args.out, config_filename)

    template_env = Environment(loader=FileSystemLoader('/'))
    template = template_env.get_template(os.path.abspath(args.default))
    generated_text = template.render(
        target=args.target,
        clang=args.clang,
        sysroot=args.sysroot
    )
    with open(config_file, "w") as outfile:
        outfile.write(generated_text)

if __name__ == '__main__':
    main()
